<template>
  <div class="app-container">
    <!-- 查询模块 -->
    <div class="formDiv">
      <el-form ref="form" :inline="true" :model="form" label-width="130px">
        <el-row>
          <el-col :span="6">
            <el-form-item label="公司全称">
              <el-input v-model="form.officeName" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="公司简称">
              <el-input v-model="form.officeNickName" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="总经理姓名">
              <el-input v-model="form.managerName" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="总经理手机">
              <el-input v-model="form.managerPhone" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item>
              <el-button type="primary" @click="searchFn()">查询</el-button>
              <el-button
                v-if="hasPermission('AdminBranchOfficeProvider.createBranchOffice')"
                type="primary"
                @click="toRoutes('FilialeFOAdd')"
              >添加</el-button>
              <el-button type="primary" @click="exportBtn(1)">导出医院业绩</el-button>
              <el-button type="primary" @click="exportBtn(2)">导出招商业绩</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <!-- 主页列表 -->
    <div v-loading="tableLoading" class="listDiv">
      <!-- table列表 -->
      <el-table :data="tableData" style="width: 100%">
        <el-table-column fixed type="index" label="序列" />
        <el-table-column fixed prop="name" label="公司全称" />
        <el-table-column prop="nick" label="公司简称" />
        <el-table-column prop="province" label="地区" />
        <el-table-column prop="type" label="分公司类型">
          <template slot-scope="scope">{{ scope.row.type|typeFilter }}</template>
        </el-table-column>
        <el-table-column prop="managerName" label="总经理姓名" />
        <el-table-column prop="managerPhone" label="总经理手机">
          <template slot-scope="scope">
            <div>{{scope.row.managerPhone | phonereg }}</div>
          </template>
        </el-table-column>
        <!-- <el-table-column prop="province" label="省" />
        <el-table-column prop="city" label="市" />-->
        <el-table-column prop="foundingTime" label="成立时间" />
        <el-table-column prop="createdAt" label="添加时间" />
        <el-table-column prop="unionUser" label="联创配额/已使用">
          <template slot-scope="scope">
            <p v-if="scope.row.type==0" />
            <p v-else>{{ scope.row.unionUser }}</p>
          </template>
        </el-table-column>
        <el-table-column prop="generalManager" label="总经理配额/已使用">
          <template slot-scope="scope">
            <p v-if="scope.row.type==0" />
            <p v-else>{{ scope.row.generalManager }}</p>
          </template>
        </el-table-column>
        <el-table-column prop="totalBalance" label="累计缴款金额">
          <template slot-scope="scope">
            <p v-if="scope.row.type==0" />
            <p v-else>{{ scope.row.totalBalance }}</p>
          </template>
        </el-table-column>
        <!-- <el-table-column prop="operator" label="操作人" /> -->
        <el-table-column align="center" fixed="right" width="100" label="操作">
          <template slot-scope="scope">
            <el-button
              v-if="hasPermission('AdminBranchOfficeProvider.deleteBranchOffice')"
              type="text"
              @click="deleteList(scope.row.id)"
            >删除</el-button>
            <el-button
              v-if="hasPermission('AdminBranchOfficeProvider.updateBranchOffice')"
              type="text"
              @click="toRoutes('FilialeFODetail',scope.row.id)"
            >详情</el-button>
            <el-button
              v-if="hasPermission('AdminBranchOfficeProvider.updateBranchOffice')"
              type="text"
              @click="toRoutes('FilialeFOEdit',scope.row.id)"
            >编辑</el-button>
            <el-button
              v-if="scope.row.type!=0"
              type="text"
              @click="toRoutes('paymentDetails',scope.row.name)"
            >缴款记录</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="pagesCon">
        <div>
          <el-pagination
            :current-page="listForm.pageNum"
            :page-sizes="page_sizes"
            :page-size="listForm.limit"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page_totals"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
      <!-- 导出业绩弹窗 -->
      <el-dialog :title="title" :visible.sync="dialogbranch" width="468px" @close="closeBranch">
        <el-form
          ref="branchForm"
          :model="branchData"
          :rules="rules"
          :inline="true"
          label-width="120px"
          label-position="right"
        >
          <el-form-item label="选择分公司" prop="officeId">
            <el-select v-model="branchData.officeId " clearable>
              <el-option
                v-for="item in BranchOption"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="选择时间" prop="time">
            <el-date-picker
              v-model="branchData.time"
              value-format="yyyy-MM-dd HH:mm:ss"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :default-time="['00:00:00', '23:59:59']"
            >></el-date-picker>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button type="primary" @click="BranchSure">确认导出</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import {
  filialeFOIndexList,
  filialeFOIndexListDelete,
  exportHospitalAchieveList,
  exportOfficeAchieve
} from "@/api/financialOther";
import { getBranch } from "@/api/paymentDetails";
import { exportfile } from "@/api/exporfile";
export default {
  name: "FilialeFOIndex",
  filters: {
    // 分公司类型 0:省分公司、1:省运营中心、2:市运营中心
    typeFilter(e) {
      switch (e) {
        case 0:
          return "省分公司";
        case 1:
          return "省运营中心";
        case 2:
          return "市运营中心";
        default:
          return "暂无";
      }
    }
  },
  data() {
    return {
      tableLoading: true,
      dialogbranch: false,

      form: {
        officeName: "",
        officeNickName: "",
        managerName: "",
        managerPhone: "",
        pageNum: 1,
        pageSize: 10,
        types: [0, 1, 2]
      },
      branchData: {
        officeId: "",
        time: [],
        createdAtStart: "",
        createdAtEnd: ""
      },
      tableData: [],
      title: null,
      exportType: null,
      BranchOption: [],
      page_sizes: [10, 30, 50, 100],
      page_totals: 60,
      listForm: {
        pageNum: 1,
        pageSize: 10
      },
      rules: {
        officeId: [
          { required: true, message: "请选择公司", trigger: "change" }
        ],
        time: [
          { required: true, message: "请选择时间", trigger: ["change", "blur"] }
        ]
      }
    };
  },
  watch: {
    "branchData.time"(val) {
      console.log(val);
      this.branchData.createdAtStart = this.branchData.time
        ? this.branchData.time[0]
        : "";
      this.branchData.createdAtEnd = this.branchData.time
        ? this.branchData.time[1]
        : "";
    }
  },
  mounted() {
    this.getList(this.form);
    this.getBranch();
  },
  methods: {
    // 按钮权限
    hasPermission(e) {
      return window.helper.hasPermission(e);
    },

    // 列表数据获取
    getList(e) {
      this.tableLoading = true;
      const datas = e;
      filialeFOIndexList(datas)
        .then(res => {
          // console.log(res)
          if (!res.data.code) {
            // console.log(res)
            this.tableData = res.data.data.list;
            this.page_totals = res.data.data.count;
          } else {
            this.$message.error(res.data.msg);
          }
          this.tableLoading = false;
        })
        .catch(error => {
          this.$message.error(error);
        });
    },
    // 查询
    searchFn() {
      this.form.pageNum = 1;

      this.getList(this.form);
    },
    // console.log(JSON.stringify(this.form))
    // 获取分公司
    getBranch() {
      getBranch(this.form).then(res => {
        // console.log("分公司", res);
        this.BranchOption = res.data.data;
      });
    },
    // 导出业绩按钮
    exportBtn(type) {
      this.branchData.officeId = "";
      this.branchData.time = "";
      this.dialogbranch = true;
      this.exportType = type;
      if (type == 1) {
        this.title = "导出医院业绩";
      } else {
        this.title = "导出招商业绩";
      }
      // console.log(type);
    },
    // 关闭弹窗
    closeBranch() {
      this.dialogbranch = false;
      this.$refs.branchForm.clearValidate();
    },
    // 导出弹窗确认
    BranchSure() {
      const Hospitaldatas = {
        officeId: this.branchData.officeId,
        createdAtStart: this.branchData.createdAtStart,
        createdAtEnd: this.branchData.createdAtEnd
      };
      const Officedatas = {
        officeId: this.branchData.officeId,
        startTime: this.branchData.createdAtStart,
        endTime: this.branchData.createdAtEnd
      };
      console.log(Hospitaldatas);
      console.log(Officedatas);
      if (this.exportType == 1) {
        exportHospitalAchieveList(Hospitaldatas).then(res => {
          exportfile(res);
          this.dialogbranch = false;
        });
      } else {
        exportOfficeAchieve(Officedatas).then(res => {
          exportfile(res);
          this.dialogbranch = false;
        });
      }
    },
    // 删除
    deleteList(e) {
      const datas = { id: e };
      this.$confirm("删除该条数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          filialeFOIndexListDelete(datas).then(res => {
            if (!res.data.code) {
              this.succStatus("删除");
              this.getList(this.form);
            } else {
              this.$message.error(res.data.msg);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    // 成功状态弹窗
    succStatus(txt) {
      this.$message({
        message: txt + "成功",
        type: "success"
      });
    },
    // 失败状态弹窗
    errorStatus(txt) {
      this.$message.error(txt + "失败");
    },
    // 路由跳转
    toRoutes(e, id) {
      // console.log(id)
      const route = { name: e, query: { id: id } };
      this.$router.push(route);
    },
    // 改变页面条数
    handleSizeChange(val) {
      this.form.pageSize = val;
      this.getList(this.form);
    },
    // 选择页数
    handleCurrentChange(val) {
      this.form.pageNum = val;
      this.getList(this.form);
    }
  }
};
</script>
<style scoped>
/* 分页 */
.pagesCon {
  height: 40px;
  margin-top: 20px;
}
.pagesCon > div {
  float: right;
}
/* 表单样式修改 */
.el-range-editor--medium.el-input__inner {
  width: 236px;
}
.el-form-item .el-form-item--medium .el-form-item__content {
  z-index: 9;
  max-width: 100% !important;
}
</style>
